package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Service
@SuppressWarnings({"all"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

	@Autowired
	private RechargeInfoMapper rechargeInfoMapper;

	@Autowired
	private UserAccountMapper userAccountMapper;

	@Autowired
	private UserAccountService userAccountService;

	/**
	 * 提交充值
	 * @param rechargeInfoVo
	 * @param userId
	 * @return
	 */
	@Override
	public String submitRecharge(RechargeInfoVo rechargeInfoVo, Long userId) {
		//保存充值信息
		RechargeInfo rechargeInfo = new RechargeInfo();
		rechargeInfo.setUserId(userId);
		String orderNo= UUID.randomUUID().toString().replaceAll("-","");
		rechargeInfo.setOrderNo(orderNo);
		rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
		rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
		rechargeInfo.setPayWay(SystemConstant.ORDER_PAY_WAY_WEIXIN);
		rechargeInfoMapper.insert(rechargeInfo);
		//返回订单号
		return orderNo;
	}

	/**
	 * 修改充值状态
	 * @param orderNo
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateRechargeInfo(String orderNo) {
		RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(new LambdaQueryWrapper<RechargeInfo>().eq(RechargeInfo::getOrderNo,orderNo));
		if(null != rechargeInfo && rechargeInfo.getRechargeStatus().equals(SystemConstant.ORDER_STATUS_UNPAID)){
			rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_PAID);
			this.updateById(rechargeInfo);

			//	需要增加账户余额;
			//	update user_account set total_amount = total_amount + ? , available_amount =available_amount+?, total_income_amount = total_income_amount + ? where user_id = ? and is_deleted = 0
			userAccountMapper.addAccountAmount(rechargeInfo.getRechargeAmount(),rechargeInfo.getUserId());
			//	需要记录充值信息
			userAccountService.addUserAccountDetail(rechargeInfo.getUserId(),"余额充值",SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT, rechargeInfo.getRechargeAmount(), rechargeInfo.getOrderNo());
		}





	}

	/**
	 * 根据订单号获取充值信息
	 * @param orderNo
	 * @return
	 */
	@Override
	public RechargeInfo getRechargeInfo(String orderNo) {
		RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(new LambdaQueryWrapper<RechargeInfo>().eq(RechargeInfo::getOrderNo, orderNo));
		return rechargeInfo;
	}
}
